package com.yifi.system.controller;

import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.DateUtils;
import com.yifi.common.utils.DbUtils;
import com.yifi.common.utils.ToolSession;

import cn.hutool.core.util.IdUtil;

@Path(value = "/SysDictType", viewPath = "/")
public class SysDictTypeController extends BaseController {
	public void index() {
		String c_pro = ToolSession.getUser(this).getC_pro();
		set("c_pro", c_pro);
		render("system/dict/type/type.html");
	}

	public void getData() {
		String id = getPara("id", "");

		String sql = "select * from sys_dict_type where dict_id=?";
		Record record = Db.findFirst(sql, id);
		if (record == null) {
			record = new Record();
			record.set("status", "0");
		}
		renderJson(record);
	}

	public void list() {
		String sqlSelect = "select * ";

		String sqlExcept = " from sys_dict_type order by dict_type ";
		renderJson(getDataTable(sqlSelect, sqlExcept));
	}

	public void add() {
		render("system/dict/type/edit.html");
	}

	public void edit() {
		String dict_id = getPara("id", "");
		set("dict_id", dict_id);
		render("system/dict/type/edit.html");
	}

	public void save() {
		String dict_id = getPara("dict_id", "");
		String dict_name = getPara("dict_name", "");
		String dict_type = getPara("dict_type", "");
		String remark = getPara("remark", "");
		String status = getPara("status", "0");

		String sql = "select * from sys_dict_type where dict_id=?";
		Record record = Db.findFirst(sql, dict_id);
		boolean isedit = true;
		if (record == null) {
			isedit = false;
			record = new Record();
			record.set("dict_id", IdUtil.fastUUID());
			record.set("create_by", ToolSession.getUser(this).getCname());
			record.set("create_time", DateUtils.getNowDate());
		}
		record.set("dict_name", dict_name);
		record.set("dict_type", dict_type);
		record.set("remark", remark);
		record.set("status", status);

		if (DbUtils.save("sys_dict_type", "dict_id", record, isedit)) {
			renderJson(success("保存成功！"));
		} else {
			renderJson(error("保存失败！"));
		}
	}

	public void remove() {
		String ids = getPara("ids", "");
		String[] idArr = ids.split(",");
		ids = "''";
		for (String string : idArr) {
			ids += ",'" + string + "'";
		}
		String sql = "delete from sys_dict_type where dict_id in (" + ids + ")";
		if (Db.update(sql) > 0) {
			renderJson(success("删除成功！"));
		} else {
			renderJson(error("删除失败！"));
		}

	}
}
